package com.liufh.common;

import lombok.Data;

import javax.servlet.http.HttpServletResponse;

/**
 * 返回样式封装
 *
 * @author: liufh
 * @create: 2022-01-21 10:00
 */
@Data
public class ResultData {

    private String code;

    private String msg;

    private Object data;

    public static ResultData success(HttpServletResponse response, Object data) {
        return resultData(response, "200", "成功", data);
    }

    public static ResultData success(HttpServletResponse response, Object data, String msg) {
        return resultData(response, "200", msg, data);
    }

    public static ResultData success(HttpServletResponse response, Object data, String msg, String code) {
        return resultData(response, code, msg, data);
    }

    public static ResultData fail(HttpServletResponse response, String code, String msg) {
        return resultData(response, code, msg, null);
    }

    public static ResultData fail(HttpServletResponse response, String code, String msg, Object data) {
        return resultData(response, code, msg, data);
    }

    private static ResultData resultData(HttpServletResponse response, String code, String msg, Object data) {
        response.addHeader("access-control-allow-origin", "*");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        ResultData resultData = new ResultData();
        resultData.setCode(code);
        resultData.setMsg(msg);
        resultData.setData(data);
        return resultData;
    }

}
